﻿#include "DeviceDlg.h"
#include "ui_devicedlg.h"
#include <QByteArray>


DeviceDlg::DeviceDlg(unsigned int ParentId, QWidget *parent) : QDialog(parent), ui(new Ui::DeviceDlg)
{
    ui->setupUi(this);
	//setFixedSize(660, 570);
	setFixedSize(651, 598);
	setWindowTitle(QString::fromLocal8Bit("从模板添加设备"));
	ui->pAfirmPushBtn->setText(QString::fromLocal8Bit("确 定"));
	ui->pCacellPushBtn->setText(QString::fromLocal8Bit("取 消"));
	ui->pDevModelEdt->setReadOnly(true);
	ui->pYxNumEdit->setReadOnly(true);
	ui->pYcNumEdit->setReadOnly(true);
	ui->pYkNumEdit->setReadOnly(true);
	ui->pYmNumEdit->setReadOnly(true);
	ui->pBhycNumEdit->setReadOnly(true);
	ui->pParaNumEdit->setReadOnly(true);
	ui->pPrecNumEdit->setReadOnly(true);
	ui->pRybNumEdit->setReadOnly(true);

	m_DevModelId = 0;
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_BhycNum = 0;
	m_PrecNum = 0;
	m_RybNum = 0;
	m_ProtectType = 0;
	m_ParentId = 0;
	m_RtuId = 0;
	m_DeviceAddr = 0;
	m_ParentId = ParentId;

	m_bAppendDevNameFlag = false;
	m_bAddYxStateFlag = false;
	m_bAddGooseFlag = false;

	InitListShow();
	GetDeviceModelInfo();
	connect(ui->pDevAddrEdit, SIGNAL(editingFinished()), this, SLOT(DeviceAddrEdit()));
	connect(ui->pAfirmPushBtn, SIGNAL(clicked()), this, SLOT(CheckDeviceParam()));
	connect(ui->pCacellPushBtn, SIGNAL(clicked()), this, SLOT(close()));
//	m_pColumnInfo = NULL;
//	m_pDataOffset = NULL;
//	m_columnNum = 0;
//	m_RecordLength =0;
	setModal(true);
}

DeviceDlg::~DeviceDlg()
{
	m_ColInfo.ReleasColumnInfo();
//	if (m_pColumnInfo)
//	{
//		delete [](char*)m_pColumnInfo;
//		m_pColumnInfo = NULL;
//	}
//	if (m_pDataOffset)
//	{
//		delete [](char*)m_pDataOffset;
//		m_pDataOffset = NULL;
//	}
    delete ui;
}

void DeviceDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//	初始化设备模板列表显示
void DeviceDlg::InitListShow()
{
	ui->pDevModelLst->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->pDevModelLst->setLineWidth(24);
	if (!ui->pDevModelLst->alternatingRowColors())
	{
		ui->pDevModelLst->setAlternatingRowColors(true);
	}
	ui->pDevModelLst->setEditTriggers(QAbstractItemView::NoEditTriggers);
	connect(ui->pDevModelLst, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(DevModelClicked(QListWidgetItem*)));
}

//	获取设备模板信息
int DeviceDlg::GetDeviceModelInfo()
{
	char *pBuff = NULL;
	int	Buff_len = 0;
	int	Ret = 0;
	unsigned int	ItemData =0;
	QString ItemText;
	QListWidgetItem *pListItem = NULL;
	ui->pDevModelLst->clear();
	QSize size(200, 19);
	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, pBuff, &Buff_len, false);
	if (Ret>0 && pBuff && Buff_len>0)
	{
		for (int i=0; i<Ret; i++)
		{
			ItemData = ((rDevModel_Info *)pBuff)[i].ID;
			pListItem = new QListWidgetItem(ui->pDevModelLst);
			ItemText = QString::fromLocal8Bit(((rDevModel_Info *)pBuff)[i].Name);
			pListItem->setText(ItemText);
			pListItem->setData(Qt::UserRole, ItemData);
			pListItem->setSizeHint(size);
			ui->pDevModelLst->addItem(pListItem);
		}
	}
	if (pBuff)
	{
		delete [](unsigned char*)pBuff;
		pBuff = NULL;
	}
	return 1;
}

void DeviceDlg::DevModelClicked(QListWidgetItem *pItem)
{
	if (!pItem)
	{
		return;
	}
	unsigned int	DevModelId = 0;
	DevModelId = pItem->data(Qt::UserRole).toUInt();
	if (m_DevModelId != DevModelId)
	{
		m_DevModelId = DevModelId;
	}
	ui->pDevModelEdt->setText("");
	if (m_DevModelId>0)
	{
		ui->pDevModelEdt->setText(pItem->text());
	}
	InitDevModeInfoShow();
}

//	显示设备模板信息
void DeviceDlg::InitDevModeInfoShow()
{
	m_YxNum = 0;
	m_YcNum = 0;
	m_YkNum = 0;
	m_YmNum = 0;
	m_SetNum = 0;
	m_BhycNum = 0;
	m_ParaNum = 0;
	m_PrecNum = 0;
	m_RybNum = 0;
	m_SetAreaNum=0;
	if (m_DevModelId > 0)
	{
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YxNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YcNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YkNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM,   m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_YmNum  );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_SetNum );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_BhycNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_ParaNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_PrecNum);
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_RybNum );
		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA,  m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO, &m_SetAreaNum );
		
	}
	ui->pYxNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YxNum));
	ui->pYcNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YcNum));
	ui->pYkNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YkNum));
	ui->pYmNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_YmNum));
	ui->pSetNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_SetNum));
	ui->pBhycNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_BhycNum));
	ui->pParaNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ParaNum));
	ui->pPrecNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_PrecNum));
	ui->pRybNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_RybNum));
}

//	编辑设备地址
void DeviceDlg::DeviceAddrEdit()
{
	int	Addr = 0;
	Addr = ui->pDevAddrEdit->text().toInt();
	if (Addr != m_DeviceAddr)
	{
		m_DeviceAddr = Addr;
		if (ui->pDevNameEdit->text().isEmpty() &&
			!ui->pDevModelEdt->text().isEmpty())
		{
			QString ByName;
			ByName = ui->pDevModelEdt->text();
			//if (g_DbCfgFunObj.IsCharacterOk(ByName))
			//{
				ui->pDevNameEdit->setText(QString::fromLocal8Bit("%1-%2-%3").arg(ByName).arg(m_ParentId%PARENT_OFFSET).arg(m_DeviceAddr));
			//}else
			//{
			//	ui->pDevNameEdit->setText(QString::fromLocal8Bit("Device-%1-%3").arg(m_DeviceAddr).arg(m_ParentId%PARENT_OFFSET));
			//}
		}
	}
}

//	判断设备参数
void DeviceDlg::CheckDeviceParam()
{
	m_DeviceName.clear();
	m_DeviceName=ui->pDevNameEdit->text();
	if (m_DeviceName.isEmpty())
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设备名称不能为空！"), QMessageBox::Ok | QMessageBox::Cancel);
		return;
	}
	if (m_DeviceName.length() > NAME_SIZE)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("设备名称太长，只允许80个字节！"), QMessageBox::Ok | QMessageBox::Cancel);
		return;
	}
	if (TABLE_RTASK_INFO == m_ParentId/PARENT_OFFSET )
	{
		if (m_DeviceAddr >0 && g_DbCfgFunObj.IsCurDevAddrRepeatInRout(m_DeviceAddr, 0, m_ParentId))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("同一任务下，不允许同类型设备地址重复！"), QMessageBox::Ok | QMessageBox::Cancel);
			return;
		}
	}
	if (InsertDevice()>0)
	{
		close();
	}
}

// void DeviceDlg::ReleasColumnInfo()
// {
// 	if (m_pColumnInfo)
// 	{
// 		delete [](char*)m_pColumnInfo;
// 		m_pColumnInfo = NULL;
// 	}
// 	if (m_pDataOffset)
// 	{
// 		delete [](int*)m_pDataOffset;
// 		m_pDataOffset = NULL;
// 	}
// 	if (m_columnNum>0)
// 	{
// 		m_columnNum = 0;
// 	}
// 	if (m_RecordLength>0)
// 	{
// 		m_RecordLength = 0;
// 	}
// }

//	插入设备
int DeviceDlg::InsertDevice()
{
	if (ui->pCheckBox_AddDevName->isChecked())
	{
		m_bAppendDevNameFlag = true;
	}else
	{
		m_bAppendDevNameFlag = false;
	}
	int	Ret = 0;
	unsigned int	RecordId = 0;
	rDevS_Info rDevice;
	bool	Is61850Model = false;
	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -1;
	}

	g_DbCfgFunObj.SetDefaultData((char*)&rDevice, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, m_ParentId);
	sprintf(rDevice.Name,"%s", m_DeviceName.toLocal8Bit().data());
	//sprintf(rDevice.ByName, "%s", m_DevByName.toLocal8Bit().data());
	rDevice.m_devaddr = m_DeviceAddr;
	rDevice.devIpA = 192*0x1000000+168*0x10000+m_DeviceAddr%0x100;
	rDevice.devIpB = 192*0x1000000+168*0x10000+1*0x100+m_DeviceAddr%0x100;
//	sprintf(rDevice.devIpA, "192.168.0.%d", m_DeviceAddr);
//	sprintf(rDevice.devIpB, "192.168.1.%d", m_DeviceAddr);
//yyyy	rDevice.IsVirDev = SYS_STATE_INVALID;
	rDevice.ParentId = m_ParentId;

	char *pbuff = NULL;
	int  buff_len = 0;
	int ret=g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_INFO, m_ParentId%PARENT_OFFSET, pbuff, &buff_len);
	if (ret>0)
	{
//yyyy		rDevice.DevType=((rTask_Info *)pbuff)->RoutType;
	}
	if (pbuff!=NULL)
	{
		delete pbuff;
		pbuff = NULL;
	}
	
	if (g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, RecordId)>0)
	{
		rDevice.ID = RecordId;
	}else
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("获取空闲设备ID失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}
	rDevice.m_modelid = m_DevModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Info");
	Ret = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, (char*)&rDevice, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, tableName);
	if (Ret<0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入设备记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		m_ColInfo.ReleasColumnInfo();
		return -5;
	}

	m_ColInfo.ReleasColumnInfo();
	
	int newYxNum = 0;
	int newYcNum = 0;
	int newYkNum = 0;
	int newYmNum = 0;
	int newRybNum = 0; 
	int newBhycNum = 0;
	int newSetNum = 0;
	int newParaNum = 0;
	int newPrecNum = 0;
	int newSetAreaNum=0;

	g_pSqLiteDB.BeginTransaction();
	newYxNum = InsertYx_Old(rDevice.ID, m_DevModelId, rDevice.Name, Is61850Model);
	newYcNum =	InsertYc_Old(rDevice.ID, m_DevModelId, rDevice.Name);
	//newYxNum = InsertYx_New(rDevice.ID, m_DevModelId, rDevice.Name, Is61850Model);
	//newYcNum =	InsertYc_New(rDevice.ID, m_DevModelId, rDevice.Name);
	newYkNum	= InsertYk_New(rDevice.ID, m_DevModelId, rDevice.Name);
	newYmNum	= InsertYm_New(rDevice.ID, m_DevModelId, rDevice.Name);
	newRybNum	= InsertRyb(rDevice.ID, m_DevModelId, rDevice.Name);
	newBhycNum	= InsertBhYc(rDevice.ID, m_DevModelId, rDevice.Name);
	newSetNum  =  InsertSet(rDevice.ID, m_DevModelId);
	newParaNum  =  InsertParam(rDevice.ID, m_DevModelId);
	newPrecNum  =  InsertPrec(rDevice.ID, m_DevModelId);

	newSetAreaNum = InserSetArea(rDevice.ID, m_DevModelId);
	g_pSqLiteDB.CommitTransaction();
	 
	emit UpDateTreeItem_Signal(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, m_ParentId, rDevice.ID, OPER_ROW_INSERT);
	emit RefreshData_Signal();

	QString Str = QString::fromLocal8Bit("插入设备“%1”: \r\n YxNum:%2,	YcNum:%3\r\n \
YkNum:%4,	YmNum:%5\r\n \
RybNum:%6,	BhycNum:%7\r\n \
SetNum:%8	ParaNum:%9\r\n \
PrecNum:%10 SetAreaNum:%11\r\n")
		.arg(QString::fromLocal8Bit(rDevice.Name))
		.arg(newYxNum)
		.arg(newYcNum)
		.arg(newYkNum)
		.arg(newYmNum)
		.arg(newRybNum)
		.arg(newBhycNum)
		.arg(newSetNum)
		.arg(newParaNum)
		.arg(newPrecNum)
		.arg(newSetAreaNum);

	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);

	return 1;
}


//	插入遥信
int  DeviceDlg::InsertYx_Old(unsigned int DeviceId, unsigned int ModelId, char *pDevName, bool b61850Model)
{
	if (m_YxNum<=0)
	{
		return 0;
	}
	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	if (ui->pCheckBox_AddStateYx->isChecked())
	{
		m_bAddYxStateFlag = true;
	}else
	{
		m_bAddYxStateFlag = false;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Yx	rYx_Info;
	int			TotalYxNum = 0;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YxName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yx");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	//g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, RecStartId);
	g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, tableName, InsertHeadSqlStr);

	if (m_YxNum>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN);
		sprintf(m_sql, "select * from rDevModel_Yx where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YX, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			if ((PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, tableName, InsertHeadSqlStr);

			SqlStr.clear();
	//		g_pSqLiteDB.BeginTransaction();
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYx_Info, 0, sizeof(rDevS_Yx));
				YxName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&rYx_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, (char*)&rYx_Info, TotalYxNum);
				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, RecStartId);
				rYx_Info.ID = RecStartId;
				rYx_Info.IndexNo = ((rDevModel_Yx*)pBuff)[i].IndexNo;
				rYx_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYx_Info.m_fun = ((rDevModel_Yx*)pBuff)[i].Fun;
				rYx_Info.m_inf = ((rDevModel_Yx*)pBuff)[i].Inf;
				rYx_Info.m_yx_type = ((rDevModel_Yx*)pBuff)[i].YxType;

				if (m_bAppendDevNameFlag)
				{
					YxName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yx*)pBuff)[i].Name);
				}else
				{
					YxName = QString::fromLocal8Bit(((rDevModel_Yx*)pBuff)[i].Name);
				}

				g_DbCfgFunObj.InputStrToBuff(rYx_Info.Name, YxName, NAME_SIZE);
				memcpy(rYx_Info.RawName, ((rDevModel_Yx*)pBuff)[i].Name, BYNAME_LEN);
				rYx_Info.RawName[BYNAME_LEN-1]=0;
				g_DbCfgFunObj.GetValuesSql((char*)&rYx_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}
				SqlStr =InsertHeadSqlStr + InsertValSqlStr;
		
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					if (pBuff)
					{
						delete [](char*)pBuff;
						pBuff = NULL;
					}
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return -5;
					break;
				}
				SqlStr.clear();
				TotalYxNum++;
			}
//			g_pSqLiteDB.CommitTransaction();
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}

	}
	m_ColInfo.ReleasColumnInfo();
	return TotalYxNum;
}


//	插入遥信
int  DeviceDlg::InsertYx_New(unsigned int DeviceId, unsigned int ModelId, char *pDevName, bool b61850Model)
{
	if (m_YxNum<=0)
	{
		return 0;
	}
	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	if (ui->pCheckBox_AddStateYx->isChecked())
	{
		m_bAddYxStateFlag = true;
	}else
	{
		m_bAddYxStateFlag = false;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Yx	rYx_Info;
	int			TotalYxNum = 0;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YxName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	bool			bFirstRowFlag = true;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yx");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, RecStartId);
	g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, tableName, InsertHeadSqlStr);

	if (m_YxNum>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN);
		sprintf(m_sql, "select * from rDevModel_Yx where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YX, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			if ((PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;

			SqlStr.clear();
			for (i=0; i<Ret; i++)
			{
				
				memset((char*)&rYx_Info, 0, sizeof(rDevS_Yx));
				YxName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&rYx_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YX, (char*)&rYx_Info, TotalYxNum);

				rYx_Info.ID = RecStartId+i;
				rYx_Info.IndexNo = ((rDevModel_Yx*)pBuff)[i].IndexNo;
				rYx_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYx_Info.m_fun = ((rDevModel_Yx*)pBuff)[i].Fun;
				rYx_Info.m_inf = ((rDevModel_Yx*)pBuff)[i].Inf;
				rYx_Info.m_yx_type = ((rDevModel_Yx*)pBuff)[i].YxType;
				
				if (m_bAppendDevNameFlag)
				{
					YxName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yx*)pBuff)[i].Name);
				}else
				{
					YxName = QString::fromLocal8Bit(((rDevModel_Yx*)pBuff)[i].Name);
				}

				g_DbCfgFunObj.InputStrToBuff(rYx_Info.Name, YxName, NAME_SIZE);
				memcpy(rYx_Info.RawName, ((rDevModel_Yx*)pBuff)[i].Name, BYNAME_LEN);
				rYx_Info.RawName[BYNAME_LEN-1]=0;
				g_DbCfgFunObj.GetValuesSql((char*)&rYx_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ((SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length()) >= (SQL_MAX_LEN*2) )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}

				SqlStr += InsertValSqlStr;
				TotalYxNum++;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}

	}
	m_ColInfo.ReleasColumnInfo();
	return TotalYxNum;
}


//	插入遥测
int DeviceDlg::InsertYc_Old(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_YcNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_Yc		rYc_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YcName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	int				length = 0;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yc");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YcNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Yc where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
// 			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, RecStartId);
// 			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
// 			{
// 				if (pBuff)
// 				{
// 					delete [](char*)pBuff;
// 					pBuff = NULL;
// 				}
// 				m_ColInfo.ReleasColumnInfo();
// 				return -3;
// 			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			//g_pSqLiteDB.BeginTransaction();
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYc_Info, 0, sizeof(rDevS_Yc));
				YcName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYc_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, (char*)&rYc_Info, i);
				
				g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, RecStartId);
				if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
				{
					if (pBuff)
					{
						delete [](char*)pBuff;
						pBuff = NULL;
					}
					m_ColInfo.ReleasColumnInfo();
					return -3;
				}
				rYc_Info.ID = RecStartId;
				rYc_Info.IndexNo = ((rDevModel_Yc*)pBuff)[i].IndexNo;
				rYc_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYc_Info.m_fun = ((rDevModel_Yc*)pBuff)[i].Fun;
				rYc_Info.m_inf = ((rDevModel_Yc*)pBuff)[i].Inf;
				rYc_Info.percent = ((rDevModel_Yc*)pBuff)[i].percent;			
				rYc_Info.rating = ((rDevModel_Yc*)pBuff)[i].rating;			
				rYc_Info.ratio = ((rDevModel_Yc*)pBuff)[i].ratio;		
				rYc_Info.offset = ((rDevModel_Yc*)pBuff)[i].offset;	
				rYc_Info.High_limit = ((rDevModel_Yc*)pBuff)[i].High_limit;
				rYc_Info.Low_limit = ((rDevModel_Yc*)pBuff)[i].Low_limit;
				rYc_Info.tYCLimit = ((rDevModel_Yc*)pBuff)[i].tYCLimit;
				memcpy(rYc_Info.unit, ((rDevModel_Yc*)pBuff)[i].unit, UNIT_NAME_LEN);

				if (m_bAppendDevNameFlag)
				{	
					YcName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yc*)pBuff)[i].Name);
				}else
				{
					YcName = QString::fromLocal8Bit(((rDevModel_Yc*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rYc_Info.Name, YcName, NAME_SIZE);
				memcpy(rYc_Info.RawName, ((rDevModel_Yc*)pBuff)[i].Name, BYNAME_LEN);
				rYc_Info.RawName[BYNAME_LEN-1]=0;
				g_DbCfgFunObj.GetValuesSql((char*)&rYc_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				SqlStr = InsertHeadSqlStr+InsertValSqlStr;
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					if (pBuff)
					{
						delete [](char*)pBuff;
						pBuff = NULL;
					}
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return -5;
					break;
				}
				SqlStr.clear();

			}
			//g_pSqLiteDB.CommitTransaction();
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}

		m_ColInfo.ReleasColumnInfo();
		return m_YcNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

//	插入遥测
int DeviceDlg::InsertYc_New(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_YcNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_Yc		rYc_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YcName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	int				length = 0;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yc");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YcNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Yc where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYc_Info, 0, sizeof(rDevS_Yc));
				YcName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYc_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YC, (char*)&rYc_Info, i);

				rYc_Info.ID = RecStartId+i;
				rYc_Info.IndexNo = ((rDevModel_Yc*)pBuff)[i].IndexNo;
				rYc_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYc_Info.m_fun = ((rDevModel_Yc*)pBuff)[i].Fun;
				rYc_Info.m_inf = ((rDevModel_Yc*)pBuff)[i].Inf;
				rYc_Info.percent = ((rDevModel_Yc*)pBuff)[i].percent;			
				rYc_Info.rating = ((rDevModel_Yc*)pBuff)[i].rating;			
				rYc_Info.ratio = ((rDevModel_Yc*)pBuff)[i].ratio;		
				rYc_Info.offset = ((rDevModel_Yc*)pBuff)[i].offset;	
				rYc_Info.High_limit = ((rDevModel_Yc*)pBuff)[i].High_limit;
				rYc_Info.Low_limit = ((rDevModel_Yc*)pBuff)[i].Low_limit;
				rYc_Info.tYCLimit = ((rDevModel_Yc*)pBuff)[i].tYCLimit;
				memcpy(rYc_Info.unit, ((rDevModel_Yc*)pBuff)[i].unit, UNIT_NAME_LEN);

				if (m_bAppendDevNameFlag)
				{	
					YcName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yc*)pBuff)[i].Name);
				}else
				{
					YcName = QString::fromLocal8Bit(((rDevModel_Yc*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rYc_Info.Name, YcName, NAME_SIZE);
				memcpy(rYc_Info.RawName, ((rDevModel_Yc*)pBuff)[i].Name, BYNAME_LEN);
				rYc_Info.RawName[BYNAME_LEN-1]=0;
				g_DbCfgFunObj.GetValuesSql((char*)&rYc_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;

			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_YcNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

//	插入遥信
int  DeviceDlg::InsertRyb(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_RybNum<=0)
	{
		return 0;
	}
	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Ryb	ryb_Info;
	int			TotalRybNum = 0;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YxName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	bool			bFirstRowFlag = true;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Ryb");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, RecStartId);
	g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, tableName, InsertHeadSqlStr);

	if (m_RybNum>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN);
		sprintf(m_sql, "select * from rDevModel_Ryb where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_RYB, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			if ((PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{	
				memset((char*)&ryb_Info, 0, sizeof(rDevS_Ryb));
				YxName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&ryb_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_RYB, (char*)&ryb_Info, TotalRybNum);

				ryb_Info.ID       = RecStartId+i;
				ryb_Info.IndexNo  = ((rDevModel_Ryb*)pBuff)[i].IndexNo;
				ryb_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				ryb_Info.m_fun	  = ((rDevModel_Ryb*)pBuff)[i].Fun;
				ryb_Info.m_inf	  = ((rDevModel_Ryb*)pBuff)[i].Inf;
		
				YxName = QString::fromLocal8Bit(((rDevModel_Ryb*)pBuff)[i].Name);
				
				g_DbCfgFunObj.InputStrToBuff(ryb_Info.Name, YxName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&ryb_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}
				
				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入软压板记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
				TotalRybNum++;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入软压板记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}

	}
	m_ColInfo.ReleasColumnInfo();
	return TotalRybNum;
}


//插入遥控
int DeviceDlg::InsertYk_Old(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_YkNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 || !pDevName )
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Yk	rYk_Info;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YkName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	bool			bFirstRowFlag = true;

	char			tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yk");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YkNum>0 )
	{
		sprintf(m_sql, "select * from rDevModel_Yk where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YK, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYk_Info, 0, sizeof(rDevS_Yk));
				YkName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYk_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, (char*)&rYk_Info, i);

				rYk_Info.ID = RecStartId+i;
				rYk_Info.IndexNo =((rDevModel_Yk*)pBuff)[i].IndexNo;
				rYk_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYk_Info.m_fun = ((rDevModel_Yk*)pBuff)[i].Fun;
				rYk_Info.m_inf = ((rDevModel_Yk*)pBuff)[i].Inf;

				if (m_bAppendDevNameFlag)
				{
					YkName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yk*)pBuff)[i].Name);
				}else
				{
					YkName = QString::fromLocal8Bit(((rDevModel_Yk*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rYk_Info.Name, YkName, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rYk_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
	}
	m_ColInfo.ReleasColumnInfo();
	return m_YkNum;
}



//插入遥控
int DeviceDlg::InsertYk_New(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_YkNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 || !pDevName )
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Yk	rYk_Info;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	unsigned int	RecStartId = 0;
	QString			YkName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	bool			bFirstRowFlag = true;

	char			tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Yk");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YkNum>0 )
	{
		sprintf(m_sql, "select * from rDevModel_Yk where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YK, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYk_Info, 0, sizeof(rDevS_Yk));
				YkName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rYk_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YK, (char*)&rYk_Info, i);
				
				rYk_Info.ID = RecStartId+i;
				rYk_Info.IndexNo = ((rDevModel_Yk*)pBuff)[i].IndexNo;
				rYk_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYk_Info.m_fun = ((rDevModel_Yk*)pBuff)[i].Fun;
				rYk_Info.m_inf = ((rDevModel_Yk*)pBuff)[i].Inf;
				
				if (m_bAppendDevNameFlag)
				{
					YkName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Yk*)pBuff)[i].Name);
				}else
				{
					YkName = QString::fromLocal8Bit(((rDevModel_Yk*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rYk_Info.Name, YkName, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rYk_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
	}
	m_ColInfo.ReleasColumnInfo();
	return m_YkNum;
}


//	插入遥脉
int DeviceDlg::InsertYm_New(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_YmNum<=0)
	{
		return 0;
	}
	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	int			Ret = 0;
	int			Result = 0;
	rDevS_Ym	rYm_Info;
	char			*pBuff = NULL;
	int			buff_len = 0;
	int			i=0;
	
	unsigned int	RecStartId = 0;
	QString			YmName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	bool			bFirstRowFlag = true;
	char			tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Ym");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_YmNum>0 )
	{
		sprintf(m_sql, "select * from rDevModel_Ym where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_YM, m_sql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rYm_Info, 0, sizeof(rDevS_Ym));
				YmName.clear();
				g_DbCfgFunObj.SetDefaultData((char*)&rYm_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_YM, (char*)&rYm_Info, i);
			
				rYm_Info.ID = RecStartId+i;
				rYm_Info.IndexNo = ((rDevModel_Ym*)pBuff)[i].IndexNo;
				rYm_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rYm_Info.m_fun = ((rDevModel_Ym*)pBuff)[i].Fun;
				rYm_Info.m_inf = ((rDevModel_Ym*)pBuff)[i].Inf;
//				rYm_Info.GroupNo = ((rDevModel_Ym*)pBuff)[i].GroupNo;
//				rYm_Info.ItemNo = ((rDevModel_Ym*)pBuff)[i].ItemNo;
				//rYm_Info.Var61850YmNo = ((rDevModel_Ym*)pBuff)[i].IndexNo;

				//memcpy(rYm_Info.MmsVarName, ((rDevModel_Ym*)pBuff)[i].MmsVarName, sizeof(((rDevModel_Ym*)pBuff)[i].MmsVarName));
				if (m_bAppendDevNameFlag)
				{
					YmName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Ym*)pBuff)[i].Name);
				}else
				{
					YmName = QString::fromLocal8Bit(((rDevModel_Ym*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rYm_Info.Name, YmName, STRING_NORMAL_LEN);
				
				g_DbCfgFunObj.GetValuesSql((char*)&rYm_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥脉记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥脉记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
	}
	m_ColInfo.ReleasColumnInfo();
	return m_YmNum;
	return 0;
}

//	插入遥测
int DeviceDlg::InsertBhYc(unsigned int DeviceId, unsigned int ModelId, char *pDevName)
{
	if (m_BhycNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 || !pDevName)
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_BhYc		rbhYc_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			YcName;
	QString			DataSetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_BhYc");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_BhycNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Bhyc where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_BHYC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rbhYc_Info, 0, sizeof(rDevS_BhYc));
				YcName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rbhYc_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_BHYC, (char*)&rbhYc_Info, i);

				rbhYc_Info.ID		  = RecStartId+i;
				rbhYc_Info.IndexNo  = ((rDevModel_Bhyc*)pBuff)[i].IndexNo;
				rbhYc_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rbhYc_Info.m_fun	  = ((rDevModel_Bhyc*)pBuff)[i].Fun;
				rbhYc_Info.m_inf	  = ((rDevModel_Bhyc*)pBuff)[i].Inf;

				rbhYc_Info.percent = ((rDevModel_Bhyc*)pBuff)[i].percent;			
				rbhYc_Info.rating = ((rDevModel_Bhyc*)pBuff)[i].rating;			
				rbhYc_Info.ratio = ((rDevModel_Bhyc*)pBuff)[i].ratio;		
				rbhYc_Info.offset = ((rDevModel_Bhyc*)pBuff)[i].offset;	
				rbhYc_Info.High_limit = ((rDevModel_Bhyc*)pBuff)[i].High_limit;
				rbhYc_Info.Low_limit = ((rDevModel_Bhyc*)pBuff)[i].Low_limit;
				rbhYc_Info.tYCLimit = ((rDevModel_Bhyc*)pBuff)[i].tYCLimit;
				memcpy(rbhYc_Info.unit, ((rDevModel_Bhyc*)pBuff)[i].unit, UNIT_NAME_LEN);
				if (m_bAppendDevNameFlag)
				{	
					YcName = QString::fromLocal8Bit(pDevName)+QString::fromLocal8Bit(((rDevModel_Bhyc*)pBuff)[i].Name);
				}else
				{
					YcName = QString::fromLocal8Bit(((rDevModel_Bhyc*)pBuff)[i].Name);
				}
				g_DbCfgFunObj.InputStrToBuff(rbhYc_Info.Name, YcName, NAME_SIZE);
				g_DbCfgFunObj.InputStrToBuff(rbhYc_Info.RawName, YcName, NAME_SIZE);
				g_DbCfgFunObj.GetValuesSql((char*)&rbhYc_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_BhycNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

int DeviceDlg::InsertSet(unsigned int DeviceId, unsigned int ModelId)
{
	if (m_SetNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_SetS		rSet_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			SetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_SetS");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETS);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Set where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_SET, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETS, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETS, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rSet_Info, 0, sizeof(rDevS_SetS));
				SetName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rSet_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETS, (char*)&rSet_Info, i);

				rSet_Info.ID		  = RecStartId+i;
				rSet_Info.IndexNo  = ((rDevModel_Set*)pBuff)[i].IndexNo;
				rSet_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rSet_Info.m_fun	  = ((rDevModel_Set*)pBuff)[i].Fun;
				rSet_Info.m_inf	  = ((rDevModel_Set*)pBuff)[i].Inf;
				rSet_Info.daType = ((rDevModel_Set*)pBuff)[i].daType;			
				rSet_Info.daFormat = ((rDevModel_Set*)pBuff)[i].daFormat;			
				rSet_Info.defval = ((rDevModel_Set*)pBuff)[i].defval;		
				rSet_Info.min_val = ((rDevModel_Set*)pBuff)[i].min_val;	
				rSet_Info.max_val = ((rDevModel_Set*)pBuff)[i].max_val;
				rSet_Info.step_val = ((rDevModel_Set*)pBuff)[i].step_val;
				memcpy(rSet_Info.unit, ((rDevModel_Set*)pBuff)[i].unit, UNIT_NAME_LEN);
				
				memcpy(rSet_Info.Name, ((rDevModel_Set*)pBuff)[i].Name, NAME_SIZE);
					
				g_DbCfgFunObj.GetValuesSql((char*)&rSet_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护遥测记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入保护定值记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_SetNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;
}

int	DeviceDlg::InsertParam(unsigned int DeviceId, unsigned int ModelId)
{
	if (m_ParaNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_Param		rPara_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			SetName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_Param");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_ParaNum>0 )
	{
		sprintf(m_Dsql, "select * from rDevModel_Para where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_PARA, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rPara_Info, 0, sizeof(rDevS_Param));
				SetName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rPara_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_PARAM, (char*)&rPara_Info, i);

				rPara_Info.ID		  = RecStartId+i;
				rPara_Info.IndexNo  = ((rDevModel_Para*)pBuff)[i].IndexNo;
				rPara_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rPara_Info.m_fun	  = ((rDevModel_Para*)pBuff)[i].Fun;
				rPara_Info.m_inf	  = ((rDevModel_Para*)pBuff)[i].Inf;
				rPara_Info.daType = ((rDevModel_Para*)pBuff)[i].daType;			
				rPara_Info.daFormat = ((rDevModel_Para*)pBuff)[i].daFormat;			
				rPara_Info.defval = ((rDevModel_Para*)pBuff)[i].defval;		
				rPara_Info.min_val = ((rDevModel_Para*)pBuff)[i].min_val;	
				rPara_Info.max_val = ((rDevModel_Para*)pBuff)[i].max_val;
				rPara_Info.step_val = ((rDevModel_Para*)pBuff)[i].step_val;
				memcpy(rPara_Info.unit, ((rDevModel_Para*)pBuff)[i].unit, UNIT_NAME_LEN);

				memcpy(rPara_Info.Name, ((rDevModel_Para*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rPara_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_ParaNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}


int	DeviceDlg::InsertPrec(unsigned int DeviceId, unsigned int ModelId)
{
	if (m_PrecNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_JDXSS		rJDxss_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			PrecName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_JDXSS");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_JDXSS);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_PrecNum>0 )
	{
		
		sprintf(m_Dsql, "select * from rDevModel_Prec where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_PREC, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_JDXSS, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_JDXSS, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rJDxss_Info, 0, sizeof(rDevS_JDXSS));
				PrecName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rJDxss_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_JDXSS, (char*)&rJDxss_Info, i);

				rJDxss_Info.ID		  = RecStartId+i;
				rJDxss_Info.IndexNo  = ((rDevModel_Prec*)pBuff)[i].IndexNo;
				rJDxss_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rJDxss_Info.m_fun	  = ((rDevModel_Prec*)pBuff)[i].Fun;
				rJDxss_Info.m_inf	  = ((rDevModel_Prec*)pBuff)[i].Inf;			
				rJDxss_Info.defval = ((rDevModel_Prec*)pBuff)[i].defval;		
				rJDxss_Info.min_val = ((rDevModel_Prec*)pBuff)[i].min_val;	
				rJDxss_Info.max_val = ((rDevModel_Prec*)pBuff)[i].max_val;
				memcpy(rJDxss_Info.Name, ((rDevModel_Prec*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rJDxss_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入精度记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
					
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入精度记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_PrecNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}

int	DeviceDlg::InserSetArea(unsigned int DeviceId, unsigned int ModelId)
{
	if (m_SetAreaNum<=0)
	{
		return 0;
	}

	if (DeviceId<=0 || ModelId<=0 )
	{
		return -1;
	}

	int				Ret = 0;
	int				Result = 0;
	rDevS_SetArea	rJDxss_Info;
	char			*pBuff = NULL;
	int				buff_len = 0;
	int				i=0;
	unsigned int	RecStartId = 0;
	QString			PrecName;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	char			tableName[TABLE_ENG_NAME_LEN];
	bool			bFirstRowFlag = true;
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevS_SetArea");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETAREA);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	memset(m_Dsql, 0, SQL_MAX_LEN*2);
	if (m_SetAreaNum>0 )
	{
		
		sprintf(m_Dsql, "select * from rDevModel_SetArea where ParentId=%d order by IndexNo", ModelId+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
		Ret = g_pSqLiteDB.DB_SelectByTableId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVMODEL_SETAREA, m_Dsql, pBuff, &buff_len);
		if (Ret>0 && pBuff && buff_len>0)
		{
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETAREA, RecStartId);
			if (RecStartId<=0 || (PARENT_OFFSET-RecStartId)<Ret )
			{
				if (pBuff)
				{
					delete [](char*)pBuff;
					pBuff = NULL;
				}
				m_ColInfo.ReleasColumnInfo();
				return -3;
			}

			g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETAREA, tableName, InsertHeadSqlStr);

			bFirstRowFlag = true;
			SqlStr = InsertHeadSqlStr;
			for (i=0; i<Ret; i++)
			{
				memset((char*)&rJDxss_Info, 0, sizeof(rDevS_SetArea));
				PrecName.clear();

				g_DbCfgFunObj.SetDefaultData((char*)&rJDxss_Info, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DeviceId+PARENT_OFFSET*TABLE_RDEVS_INFO);
				g_DbCfgFunObj.SetDefaultData_Special(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_SETAREA, (char*)&rJDxss_Info, i);

				rJDxss_Info.ID		  = RecStartId+i;
				rJDxss_Info.IndexNo  = ((rDevModel_SetArea*)pBuff)[i].IndexNo;
				rJDxss_Info.ParentId = DeviceId + PARENT_OFFSET*TABLE_RDEVS_INFO;
				rJDxss_Info.m_fun	  = ((rDevModel_SetArea*)pBuff)[i].Fun;
				rJDxss_Info.m_inf	  = ((rDevModel_SetArea*)pBuff)[i].Inf;			
				rJDxss_Info.value = ((rDevModel_SetArea*)pBuff)[i].value;		
				memcpy(rJDxss_Info.Name, ((rDevModel_SetArea*)pBuff)[i].Name, NAME_SIZE);

				g_DbCfgFunObj.GetValuesSql((char*)&rJDxss_Info, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
				if (InsertValSqlStr.length()<=0)
				{
					continue;
				}

				if ( ( SqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
				{
					sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
					Result = g_pSqLiteDB.DB_Modify(m_Dsql);
					if (Result<0)
					{
						if (pBuff)
						{
							delete [](char*)pBuff;
							pBuff = NULL;
						}
						QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值区记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
						m_ColInfo.ReleasColumnInfo();
						return -5;
						break;
					}
					SqlStr.clear();
					SqlStr = InsertHeadSqlStr;
					bFirstRowFlag = true;
				}
				if (bFirstRowFlag)
				{
					bFirstRowFlag = false;
				}else
				{
					SqlStr += QString::fromLocal8Bit(",");
				}
				SqlStr += InsertValSqlStr;
			}
		}

		if (pBuff)
		{
			delete [](char*)pBuff;
			pBuff = NULL;
		}
		if (SqlStr.length()>0)
		{
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值区记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return -5;
			}
			SqlStr.clear();
		}
		m_ColInfo.ReleasColumnInfo();
		return m_SetAreaNum;
	}
	m_ColInfo.ReleasColumnInfo();
	return 0;

}